import http from "../util/httpRequest.js"

let userTest = {
	id: 1,
	username: "ogbA_44fnEu4aNnnieP0XPr_Is_0",
	name: "陈亚如-test",
	phone: "17058179046",
	address: "四川农业大学2区15舍309",
	sex: 1,
	avatar: "https://xxl-1302724632.cos.ap-shanghai.myqcloud.com/sysUserAvatar/1/QQ%E5%9B%BE%E7%89%8720210328225418.jpg",
	token: "mock-token",
}

// 微信授权登录
export function loginByWeixin(params) {
	params.method = "POST"
	params.path = "/auth/login/weixin?code=" + params.code + "&type=" + params.type
	params.testData = function() {
		if (params.type == 1) {
			return {
				state: "state-" + Math.random(),
				qrcodeImg: "",
			}
		} else if (params.type == 2) {
			return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx268a12d11f0cece5&redirect_uri=http://xxl.blue/waimai-api/auth/login/weixinH5Auth&response_type=code&scope=snsapi_base&state=h5url-xxx#wechat_redirect" // h5_url
		} else if (params.type == 3) {
			return "token-xxx"
		}
	}()
	params.showLoading = true
	http(params)
}

// 通过state获取token,电脑端微信扫码登录时
export function getTokenByState(params) {
	params.path = "/auth/login/getToken?state=" + params.state
	params.testData = Math.random() < 0.2 ? "token-xxxxx" : ""
	params.showLoading = false
	http(params)
}

// 发送手机验证码
export function sendPhoneVerifyCode(params) {
	params.path = "/auth/phoneVerifyCode?phone=" + params.phone
	params.testData = 600 // 600秒的冷却时间
	params.showLoading = true
	http(params)
}

// 手机验证码登录
export function loginByPhone(params) {
	params.method = "POST"
	params.path = `/auth/login/phone?phone=${params.phone}&code=${params.code}`
	params.testData = "token-xxx"
	params.showLoading = true
	http(params)
}

// 退出登录
export function logout(params) {
	params.method = "DELETE"
	params.path = "/user/logout"
	http(params)
}


export function getUserInfo(params) {
	params.path = "/user/info"
	params.testData = userTest
	params.showLoading = true
	http(params)
}

// 更新个人信息
export function updateUser(params) {
	params.method = "PUT"
	params.path = "/user/info"
	params.data = params.user
	params.testData = function() {
		userTest = params.user
		return true
	}()
	showLoading: true
	http(params)
}
